package com.leadership.entity.user;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.JsonIdentityReference;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
import com.leadership.entity.idm.IdmUser;
import com.leadership.entity.idm.Organization;

@Entity
@Table(name = "user")
public class User {
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    
    /** 閻€劍鍩涢崥锟�  */
    private String username;
    
    /** 娑擃厽鏋冮崥宥囆� */
    private String usercn;
    
    /** 鐎靛棛鐖�  */
    private String password;
    
    /** 閹靛婧�閸欙拷  */
    private String email;
    
    /** 閺夊啴妾�  */
    private String authority;
    
    /** 娑撳﹦楠囨慨鎾虫倳 */
    private String reporterName;
    
    /** 娑撳﹦楠囬柇顔绢唸 */
    private String reporterEmail;
    
    private String hrEmail;
     private String hrName;
    
    /** 娑撳﹦楠� */
    @ManyToOne(fetch = FetchType.LAZY, optional = true)
    @JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id")
    @JsonIdentityReference(alwaysAsId=true)
    private Reporter reporter;
    
//    /** 娑撳﹦楠� */
//    @ManyToOne(fetch = FetchType.LAZY, optional = true)
//    @JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id")
//    @JsonIdentityReference(alwaysAsId=true)
//    private TestReporter testReporter;
    
    /** 闁劑妫� */
    @ManyToOne(fetch = FetchType.LAZY, optional = true)
    @JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id")
    @JsonIdentityReference(alwaysAsId=true)
    private Organization organization;
    
    /** 閸栧搫鐓� */
    private String area;
    
    /** 閼卞矁鍏� */
    private String position;
    
    /** 閸忣偊鍎磋箛鍐╃《閺勵垰鎯佺�瑰本鍨� */
    @Column(name = "is_heart_method_completed")
    private boolean heartMethodCompleted;
    
    /** 闁艾鍙ч崷鏉挎禈閺勵垰鎯佺�瑰本鍨� */
    @Column(name = "is_map_completed")
    private boolean mapCompleted;
    
    /** 閺勵垰鎯佹＃鏍偧閻у妾� */
    @Column(name = "is_first_land")
    private boolean firstLand;
    
    private Long currentProjectId;
    
    private String userstatus;
    
    /** 閺勵垰鎯侀崥顖滄暏 */
    @Column(name = "is_disabled")
    private boolean disabled;
    
    /**
     * The default constructor
     */
    public User() {
    }
    
    /**
     * 
     * @param username
     * @param email
     * @param organization
     */
    public User(String username, String email, Organization organization) {
        this.username = username;
        this.email = email;
        this.password = encodePassword(generateDefaultPassword()); 
        this.authority = "STUDENT";
        this.organization = organization;
        this.firstLand = true;
        this.disabled = false;
    }
    
    /**
     * 
     * @param username
     * @param email
     * @param organization
     * @param currentProjectId
     */
    public User(String username, String email, Organization organization, Long currentProjectId) {
        this.username = username;
        this.email = email;
        this.password = encodePassword(generateDefaultPassword()); 
        this.authority = "STUDENT";
        this.organization = organization;
        this.currentProjectId = currentProjectId;
        this.firstLand = true;
        this.disabled = false;
    }
    
    /**
     * 
     * @param idmUser
     * @param organization
     * @param currentProjectId
     * @param reporter
     * @param reporterEmail
     */
    public User(IdmUser idmUser, Organization organization, Long currentProjectId, String reporter, String reporterEmail) {
        this.username = idmUser.getAlias();
        this.email = idmUser.getMail();
        this.position = idmUser.getJobname();
        this.usercn = idmUser.getUsercn();
        this.password = encodePassword(generateDefaultPassword()); 
        this.authority = "STUDENT";
        this.organization = organization;
        this.currentProjectId = currentProjectId;
        this.reporterName = reporter;
        this.reporterEmail = reporterEmail;
        this.firstLand = true;
        this.userstatus = idmUser.getUserstatus();
        this.disabled = false;
    }
    
    /**
     * 
     * @param username
     * @param email
     * @param organization
     * @param area
     * @param position
     */
    public User(String username, String email, Organization organization, String area, String position, String userstatus) {
        this.username = username;
        this.email = email;
        this.password = encodePassword(generateDefaultPassword()); 
        this.authority = "STUDENT";
        this.organization = organization;
        this.area = area;
        this.position = position;
        this.userstatus = userstatus;
        this.firstLand = true;
        this.disabled = false;
    }
    
    /**
     * 
     * @param username
     */
    public User(String username) {
        this.username = username;
        this.email = "809107193@qq.com";
        this.password = encodePassword(generateDefaultPassword()); 
        this.authority = "STUDENT";
        this.disabled = false;
    }
    
    public String getFullname() {
        if(this.userstatus == null) {
            return this.usercn;
        } else {
            if(this.userstatus.equalsIgnoreCase("0")) {
                return this.usercn + "(缁傛槒浜�)";
            } else {
                return this.usercn;
            }
        }
    }
    
    public String getTraineeUserstatus() {
        if(this.userstatus == null) {
            return null;
        } else {
            if(this.userstatus.equalsIgnoreCase("0")) {
                return "(缁傛槒浜�)";
            } else {
                return null;
            }
        }
    }
    
    /**
     * @param id
     * @param email
     * @param currentProjectId
     */
    public User(Long id, String email, Long currentProjectId) {
        this.email = email;
        this.id = id;
        this.currentProjectId = currentProjectId;
    }
    
    /**
     * 
     * @return
     */
    public User updateHeart() {
        this.heartMethodCompleted = true;
        return this;
    }
    
    public User updateMap() {
        this.mapCompleted = true;
        return this;
    }
    
    /**
     * using mobile's last 6 digitals as default password
     * 
     * @return
     */
    private String generateDefaultPassword() {
        return StringUtils.substring(this.email, 0, 6);
    }
    
    /**
     * 
     * @param password
     * @return
     */
    public String encodePassword(String password) {
        String saltPassword = StringUtils.join(password, "{", this.email, "}");
        return DigestUtils.md5Hex(saltPassword);
    }
    
    /**
     * 
     * @param password
     * @return
     */
    public User changePassword(String password) {
        String saltPassword = StringUtils.join(password, "{", this.email, "}");
        this.password = DigestUtils.md5Hex(saltPassword);
        return this;
    }
    
    /**
     * 
     * @param reporter
     * @return
     */
    public User bindUser(Reporter reporter) {
        this.reporter = reporter;
        return this;
    }
    
//    public User bindUser(TestReporter testReporter) {
//        this.testReporter = testReporter;
//        return this;
//    }
//    /**
//     * 
//     * @param reporter
//     * @return
//     */
//    public User bindUser(Reporter reporter,TestReporter testReporter,String hrName, String hrEmail) {
//        this.reporter = reporter;
//        this.testReporter=testReporter;
//        this.hrName=hrName;
//        this.hrEmail=hrEmail;
//        return this;
//    }
    
    
    /**
     * 
     * @param reporterName
     * @param reporterEmail
     * @return
     */
    public User edit(String reporterName, String reporterEmail) {
        this.reporterName = reporterName;
        this.reporterEmail = reporterEmail;
        return this;
    }
    
    public User edit(String reporterName, String reporterEmail,String hrName, String hrEmail) {
        this.reporterName = reporterName;
        this.reporterEmail = reporterEmail;
        this.hrEmail=hrEmail;
        this.hrName=hrName;
        return this;
    }
    
    /**
     * 
     * @param reporterName
     * @param reporterEmail
     * @param projectId
     * @return
     */
    public User edit(String reporterName, String reporterEmail, Long projectId) {
        this.reporterName = reporterName;
        this.reporterEmail = reporterEmail;
        this.currentProjectId = projectId;
        return this;
    }
    
    /**
     * 
     * @param reporterName
     * @param reporterEmail
     * @param projectId
     * @return
     */
    public User edit(String reporterName, String reporterEmail, Long projectId,String hrName,String hrEmail) {
        this.reporterName = reporterName;
        this.reporterEmail = reporterEmail;
        this.currentProjectId = projectId;
        this.hrEmail=hrEmail;
        this.hrName=hrName;
        return this;
    }
    
    /**
     * 
     * @param projectId
     * @return
     */
    public User bindProject(Long projectId) {
        this.currentProjectId = projectId;
        return this;
    }
    
    /**
     * 
     * @return
     */
    public User completeMap() {
        this.mapCompleted = true;
        return this;
    }
    
    /**
     * 
     * @return
     */
    @Transient
    public String getIdString() {
        return this.id.toString();
    }

    /**
     * @return the id
     */
    public Long getId() {
        return id;
    }

    /**
     * @return the username
     */
    public String getUsername() {
        return username;
    }

    /**
     * @return the usercn
     */
    public String getUsercn() {
        return usercn;
    }

    /**
     * @return the password
     */
    public String getPassword() {
        return password;
    }

    /**
     * @return the email
     */
    public String getEmail() {
        return email;
    }

    /**
     * @return the authority
     */
    public String getAuthority() {
        return authority;
    }

    /**
     * @return the reporterName
     */
    public String getReporterName() {
        return reporterName;
    }

    /**
     * @return the reporterEmail
     */
    public String getReporterEmail() {
        return reporterEmail;
    }

    /**
     * @return the reporter
     */
    public Reporter getReporter() {
        return reporter;
    }

    /**
     * @return the organization
     */
    public Organization getOrganization() {
        return organization;
    }

    /**
     * @return the area
     */
    public String getArea() {
        return area;
    }

    /**
     * @return the position
     */
    public String getPosition() {
        return position;
    }

    /**
     * @return the heartMethodCompleted
     */
    public boolean isHeartMethodCompleted() {
        return heartMethodCompleted;
    }

    /**
     * @return the mapCompleted
     */
    public boolean isMapCompleted() {
        return mapCompleted;
    }

    /**
     * @return the firstLand
     */
    public boolean isFirstLand() {
        return firstLand;
    }

    /**
     * @return the currentProjectId
     */
    public Long getCurrentProjectId() {
        return currentProjectId;
    }
    
    public String getUserstatus() {
        return userstatus;
    }

    /**
     * @return the disabled
     */
    public boolean isDisabled() {
        return disabled;
    }

    /**
     * @param id the id to set
     */
    public void setId(Long id) {
        this.id = id;
    }

    /**
     * @param username the username to set
     */
    public void setUsername(String username) {
        this.username = username;
    }

    /**
     * @param usercn the usercn to set
     */
    public void setUsercn(String usercn) {
        this.usercn = usercn;
    }

    /**
     * @param password the password to set
     */
    public void setPassword(String password) {
        this.password = password;
    }

    /**
     * @param email the email to set
     */
    public void setEmail(String email) {
        this.email = email;
    }

    /**
     * @param authority the authority to set
     */
    public void setAuthority(String authority) {
        this.authority = authority;
    }

    /**
     * @param reporterName the reporterName to set
     */
    public void setReporterName(String reporterName) {
        this.reporterName = reporterName;
    }

    /**
     * @param reporterEmail the reporterEmail to set
     */
    public void setReporterEmail(String reporterEmail) {
        this.reporterEmail = reporterEmail;
    }

    /**
     * @param reporter the reporter to set
     */
    public void setReporter(Reporter reporter) {
        this.reporter = reporter;
    }

    /**
     * @param organization the organization to set
     */
    public void setOrganization(Organization organization) {
        this.organization = organization;
    }

    /**
     * @param area the area to set
     */
    public void setArea(String area) {
        this.area = area;
    }

    /**
     * @param position the position to set
     */
    public void setPosition(String position) {
        this.position = position;
    }

    /**
     * @param heartMethodCompleted the heartMethodCompleted to set
     */
    public void setHeartMethodCompleted(boolean heartMethodCompleted) {
        this.heartMethodCompleted = heartMethodCompleted;
    }

    /**
     * @param mapCompleted the mapCompleted to set
     */
    public void setMapCompleted(boolean mapCompleted) {
        this.mapCompleted = mapCompleted;
    }

    /**
     * @param firstLand the firstLand to set
     */
    public void setFirstLand(boolean firstLand) {
        this.firstLand = firstLand;
    }

    /**
     * @param currentProjectId the currentProjectId to set
     */
    public void setCurrentProjectId(Long currentProjectId) {
        this.currentProjectId = currentProjectId;
    }
    
    public void setUserstatus(String userstatus) {
        this.userstatus = userstatus;
    }

    /**
     * @param disabled the disabled to set
     */
    public void setDisabled(boolean disabled) {
        this.disabled = disabled;
    }

	public String getHrEmail() {
		return hrEmail;
	}

	public void setHrEmail(String hrEmail) {
		this.hrEmail = hrEmail;
	}

	public String getHrName() {
		return hrName;
	}

	public void setHrName(String hrName) {
		this.hrName = hrName;
	}

//	public TestReporter getTestReporter() {
//		return testReporter;
//	}
//
//	public void setTestReporter(TestReporter testReporter) {
//		this.testReporter = testReporter;
//	}

}
